// components/my-login/my-login.js
const config = require(`../../config.js`);
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    city: '',
    province: '',
    avatarUrl: '',
    gender: '',
    nickName: '',
   
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserMess(res) {

      let that = this
      if (res.detail.errMsg == 'getUserInfo:ok') {
        wx.setStorageSync('userMessage', res.detail.userInfo)
        this.setData({
          city: res.detail.userInfo.city,
          province: res.detail.userInfo.province,
          avatarUrl: res.detail.userInfo.avatarUrl,
          gender: res.detail.userInfo.gender,
          nickName: res.detail.userInfo.nickName,
          userMessage: res.detail.userInfo
        })
        wx.login({
          success: rest => {
            wx.request({
              url: `${config.url}/admin/user/checkUser`,
              data: {
                wxCode: rest.code,

              },
              header: {
                'content-type': 'application/json',
              },
              method: 'POST',
              success: function (res) {
                if (res.data.result == true) {
                  wx.login({
                    success: rest => {
                      wx.request({
                        url: `${config.url}/admin/user/login`,
                        data: {
                          wxCode: rest.code,
                          loginType: '1',
                        },
                        header: {
                          'content-type': 'application/json',
                        },
                        method: 'POST',
                        success: async function (res) {
                          if (res.data.result == true) {
                            if (res.data.code == 99) {
                              //本地存储
                              await wx.setStorageSync("token", res.data.data)
                              //城市Id
                              //codeNum
                              await wx.setStorageSync("codeNum", res.data.code)
                              wx.setStorageSync("userMess", res.data.object)
                              wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                              wx.setStorageSync("phone", res.data.object.phone)
                              await wx.setStorageSync("openId", res.data.body.openid)
                              //0是别人登陆 1是商家登陆 2个人登陆
                              wx.setStorageSync("type", res.data.object.type)

                              wx.reLaunch({
                                url: '../index/index',
                              })
                            }
                          } else {
                            if (res.data.code == 103 || res.data.code == 105) {
                              //本地存储
                              await wx.setStorageSync("token", res.data.data)
                              wx.setStorageSync("phone", res.data.object.phone)
                              wx.setStorageSync("openId", res.data.body.openid)
                              //城市Id
                              //codeNum
                              await wx.setStorageSync("codeNum", res.data.code)
                              wx.setStorageSync("userMess", res.data.object)
                              await wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                              wx.navigateTo({

                                url: '../type/type',
                              })
                            }
                            else if (res.data.code == 104) {
                              //本地存储
                              await wx.setStorageSync("token", res.data.data)
                              wx.setStorageSync("phone", res.data.object.phone)
                              wx.setStorageSync("openId", res.data.body.openid)
                              //城市Id
                              //codeNum
                              await wx.setStorageSync("codeNum", res.data.code)
                              wx.setStorageSync("userMess", res.data.object)
                              wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                              wx.navigateTo({

                                url: '../shopBack/shopBack',
                              })
                            }
                          }
                        }
                      })

                    }
                  })
                } else {

                  wx.navigateTo({
                    url: '../login/login?city=' + that.data.city + '&province=' + that.data.province + '&avatarUrl=' +
                      that.data.avatarUrl + '&gender=' + that.data.gender + '&nickName=' + that.data.nickName + '&cityId=' + that.data.cityId + '&referee=' + that.data.referee,
                  })
                }
              }
            })

          }
        })


      } else {
        wx.navigateBack({
          delta: 0
        })
      }
    },
  }
})